﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.SiteAdmin;
using Jzy.Model.SiteAdmin;
using Jzy.Core.Attach;

using System.IO;
using Jzy.Core.Utility;
using Jzy.Common;

namespace Jzy.Web.SiteAdmin.Picture
{
    public partial class PictureBatchAdd : PageEdit
    {
        private ArticleBO _article;
        private ArticleBO Article
        {
            get
            {
                if (_article == null)
                {
                    _article = new ArticleBO();
                }
                return _article;
            }
        }

        private string MenuID
        {
            get
            {
                return Request.QueryString["MenuID"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.Enctype = "multipart/form-data";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //不能用foreach，原因不明
                for (int i = 0; i < Request.Files.Count;i++ )
                {
                    HttpPostedFile file = Request.Files[i];

                    string extention = System.IO.Path.GetExtension(file.FileName).ToLower();
                    if ((extention == ".jpg" || extention == ".png" || extention == ".gif" || extention == ".bmp") && file.ContentLength > 0)
                    {
                        ArticleInfo info = new ArticleInfo();

                        this.Article.SetDelegate(info);

                        info.MenuID = int.Parse(MenuID);
                        info.MenuName = new MenuBO().GetName(MenuID);

                        info.InputTime = DateTime.Now;

                        info.UpdateTime = DateTime.Now;
                        info.CreatorID = AppSession.UserID;
                        info.CreatorName = AppSession.UserName;
                        info.IsDisplay = true;
                        info.Name = Path.GetFileNameWithoutExtension(file.FileName);

                        this.Article.SaveImage(info, file);
                       

                        this.Article.Save(info);
                    }
                }
                JSUtil.RefreshView();
            }
            catch (Exception ex)
            {
                Label lbl = new Label();
                lbl.Text = ex.Message;
                pnlMain.Controls.Clear();
                pnlMain.Controls.Add(lbl);
            }
        }
    }
}
